class player extends egret.Sprite {
	public constructor() {
		super();
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addToStage, this);
	}
	// 动画帧工厂
	private pf;
	// 动画帧人物
	private p1;
	// 人物扑街动画
	private rollTween;
	// 人物动画状态
	private movieState;
	// 下落计时器 , 跳起计时器 , 滚动计时器
	public downerTimer; upperTimer; rollTimer;
	// 可跳起次数
	public jumpCount = 2;
	// 状态： 自动下落			可跑步			动画状态可变
	public autoFall = true; canRun = false; canChange = true;
	// 首次登场
	public first = true;


	addToStage() {
		// 初始化 动画帧
		let stageW = this.stage.stageWidth;
		let stageH = this.stage.stageHeight;
		// 首次登场 
		if (this.first) {
			this.pf = new egret.MovieClipDataFactory(RES.getRes("player_json"), RES.getRes("player_png"))
			this.p1 = new egret.MovieClip(this.pf.generateMovieClipData("run"));


			this.width = this.p1.width;
			this.height = this.p1.height;
			this.scaleX = 1.3;
			this.scaleY = 1.3;

			this.addChild(this.p1);
			this.first = false;
		}




		this.x = 350;
		this.y = 200;


		// this.running();
		let self = this;
		this.rolling(12, function () {

			self.p1.movieClipData = self.pf.generateMovieClipData("run");
			self.p1.gotoAndPlay("jump", -1);
			self.movieState = "jump";
		});


		// 登场动画
		// egret.Tween.get(this, { loop: false }).to({
		// 	y: (stageH * .2)
		// }, 100).call(() => {
		// this.canRun = true;
		// this.autoFall = true;
		// this.down();
		// this.p1.movieClipData = this.pf.generateMovieClipData("run");
		// this.p1.gotoAndPlay("jump", -1);
		// this.movieState = "jump";
		// });

	}

	// 切换跑步状态
	running() {
		// console.log("running")
		if (this.rollTween) {
			// 移除人物翻滚
			egret.Tween.removeTweens(this.p1);
			this.rollTween = null;
		}
		if (this.downerTimer) {
			// console.log("停止定时器")
			this.downerTimer.stop();
			this.downerTimer = null;
		}
		this.p1.rotation = 0;
		// this.p1.movieClipData = null; 
		// console.log("动画状态：", this.movieState)
		if (this.movieState !== "running") {
			this.movieState = "running";
			// console.log("切换动画")
			this.p1.movieClipData = this.pf.generateMovieClipData("run");
			this.p1.gotoAndPlay("running", -1);
		}

		// 可跳次数恢复
		this.jumpCount = 2;
		// 自由落体 跑步状态即使在台阶上，可以开始自由落体
		this.autoFall = true;
	}
	// 切换扑街动画
	rolling(t, callback) {
		if (!t) {
			t = 50;
		}
		if (!this.rollTimer) {

			this.movieState = "roll";
			// this.autoFall = false;
			this.canRun = false;
			this.p1.movieClipData = this.pf.generateMovieClipData("roll");

			this.rollTimer = new egret.Timer(15, t);
			this.rollTimer.addEventListener(egret.TimerEvent.TIMER, function () {
				this.p1.rotation += 21.6;
			}, this);

			this.rollTimer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, function () {
				this.p1.rotation = 0;
				this.rollTimer.stop();
				this.rollTimer = null;

				this.autoFall = true;
				this.canRun = true;
				this.movieState = "";
				// console.log("roll完了");

				if (callback) {
					callback()
				}

			}, this);
			this.rollTimer.start();
		}

	}
	// 跳起
	jump() {
		// console.log("jumpCount", this.jumpCount)

		if (this.jumpCount > 0) {

			this.jumpCount--;
			this.p1.movieClipData = this.pf.generateMovieClipData("run");
			this.p1.gotoAndPlay("jump", -1);
			this.movieState = "jump";

			// 清除跳起定时器
			if (this.upperTimer) {
				this.upperTimer.stop();
				this.upperTimer = null;
			}

			// 清除下落定时器
			if (this.downerTimer) {
				this.downerTimer.stop();
				this.downerTimer = null;
			}

			var t = 500;
			// var loop = t / 50;
			var loop = 30;
			this.upperTimer = new egret.Timer(16, loop);
			// 跳起，不断减少y值
			this.upperTimer.addEventListener(egret.TimerEvent.TIMER, function () {
				this.autoFall = false;

				switch (true) {
					case (this.upperTimer.currentCount < loop - 6):
						this.y -= 15;
						break;	
					case (this.upperTimer.currentCount < loop - 3):
						this.y -= 5;
						break;	
				}



			}, this);
			// 跳起结束的回调，当到最高点就开始下落
			this.upperTimer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, function () {

				this.upperTimer.stop();
				this.upperTimer = null;

				this.autoFall = true;
				this.down(0);

			}, this);
			this.upperTimer.start();
		}

	}
	// 判定y轴
	judge() {
		let p1Y = this.p1.y + this.p1.height;
		// console.log(this.$stage)
	}
	// 下落
	down(t = 0) {
		// console.log("autoFall", this.autoFall)

		if (this.autoFall && !this.downerTimer) {

			// console.log("down ！！！！！！canRun", this.canRun)

			if (this.canRun) {
				// console.log("动作切换 ！！！！！！canRun", this.canRun)
				// 动作切换 区分在下落情况下 可以跳跃的动作 和 滚动时不可控制的动作
				this.p1.movieClipData = this.pf.generateMovieClipData("run");
				this.p1.gotoAndPlay("jump", -1);
				this.movieState = "jump";
			}

			this.downerTimer = null;

			var loop = t / 50;
			this.downerTimer = new egret.Timer(25, loop);
			this.downerTimer.addEventListener(egret.TimerEvent.TIMER, function () {
				this.y += 25;
			}, this);
			this.downerTimer.start();
		}
	}
	/** 出界	
	 *	1.滚动的定时器和逻辑都要提前结束
	 	2.
	 ***/
	outSide() {
		if (this.rollTimer) {
			this.rollTimer.stop();
			this.rollTimer = null;
		}
		// 初始化状态值
		this.canRun = true;
		this.autoFall = true;

		this.addToStage();


	}

}